﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Diagnostics;
using SoftwareLogistics.SharepointClient.Models;
using SoftwareLogistics.SharepointClient.Library;
using SoftwareLogistics.SharepointClient.Library.Models;

namespace SoftwareLogistics.SharepointClient
{
    public partial class MainPage : PhoneApplicationPage
    {

        private const string STR_UserName = "UserName";
        private const string STR_UsersPlainTextPassword = "UsersPlainTextPassword";
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Common.AuthService.Authenticated += (sender, args) =>
            {
                Common.Users.GetUsers();

                Dispatcher.BeginInvoke(() =>
                    {
                        Common.StateBag[STR_UserName] = UserName.Text;
                        if (SavePassword.IsChecked.HasValue && SavePassword.IsChecked.Value)
                            Common.StateBag[STR_UsersPlainTextPassword] = Password.Password;
                        else if (Common.StateBag.ContainsKey(STR_UsersPlainTextPassword))
                            Common.StateBag.Remove(STR_UsersPlainTextPassword);

                        Common.SaveStateBag();
                    });
            };

            Common.AuthService.FailedAuthentication += (sender, args) => 
            {
                Dispatcher.BeginInvoke(() => 
                {
                    ProgressBar.IsIndeterminate = false;
                    ProgressBar.Visibility = System.Windows.Visibility.Collapsed;
                    LoginButton.IsEnabled = true;

                    MessageBox.Show("Invalid credentials, please try again.");
                    Password.Password = string.Empty;
                    Password.Focus();
                });
            };

            Common.Users.GotUsers += (sender, args) => 
            {
                Common.SyncService.SyncNow("pm");
            };

            Common.SyncService.SyncCompleted += (sender, args) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    LoginButton.IsEnabled = true;
                    Common.Users.SetCurrentUser(UserName.Text);
                    ProgressBar.IsIndeterminate = false;
                    ProgressBar.Visibility = System.Windows.Visibility.Collapsed;
                    NavigationService.Navigate(new Uri("/Project.xaml", UriKind.Relative));
                });
            };
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (Common.StateBag.ContainsKey(STR_UserName))
                UserName.Text = Common.StateBag[STR_UserName];

            if (Common.StateBag.ContainsKey(STR_UsersPlainTextPassword))
            {
                Password.Password = Common.StateBag[STR_UsersPlainTextPassword];
                SavePassword.IsChecked = true;
            }

            if (Common.Users.CurrentUser != null)
            {
                TitlePanel.Visibility = System.Windows.Visibility.Collapsed;
                ContentPanel.Visibility = Visibility.Collapsed;

                LoggedOut.Visibility = System.Windows.Visibility.Visible;
            }

            base.OnNavigatedTo(e);
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            ProgressBar.IsIndeterminate = true;
            ProgressBar.Visibility = System.Windows.Visibility.Visible;
            LoginButton.IsEnabled = false;

            Common.AuthService.Auth(UserName.Text, Password.Password);
        }
    }
}